﻿using System.Xml;

namespace IniFileManager
{
	/// <summary>
	/// Class representing inner structure of a bool element
	/// within property defined by library user in ini schema.
	/// </summary>
	internal sealed class IniBoolElementSchema : IniElementSchema
	{
		private readonly bool defaultValue;

		/// <summary>
		/// Creates "IniBoolElementSchema" from xml node in ini schema xml representation.
		/// </summary>
		/// <param name="boolElementNode">Ini bool element schema xml node.</param>
		internal IniBoolElementSchema( XmlNode boolElementNode )
		{
			// Embedded "IniSchema.xsd"'s definition guarantees existence and format of this attribute:
			this.defaultValue = bool.Parse( boolElementNode.Attributes[IniSchemaXsd.ATTRIBUTE_DEFAULT].Value );
		}

		/// <summary>
		/// Boxed default bool value of an element into object.
		/// </summary>
		internal override object DefaulValue
		{
			get
			{
				return this.defaultValue;
			}
		}

		/// <summary>
		/// Tells wheather a value is valid within.
		/// </summary>
		/// <param name="boxedValue">Value boxed in object.</param>
		/// <returns>True if value valid within. False if not.</returns>
		internal override bool IsValueValidWithin( object boxedValue )
		{
			bool value;
			try
			{
				value = (bool)boxedValue;
			}
			catch
			{
				return false;
			}
			return true;
		}

	}
}
